package com.atguigu.carousel.service.impl;

import com.atguigu.carousel.service.CarouselService;
import com.atguigu.carousel.mapper.CarouselMapper;
import com.atguigu.pojo.Carousel;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 *
 */

@Slf4j
@Service
public class CarouselServiceImpl implements CarouselService {

    @Autowired
    private CarouselMapper carouselMapper;

    /**
     * 查询优先级最高的四条轮播图数据
     * 按照优先级查询数据库数据
     * 使用 stream流 内存数据切割 保留6条或者4条数据
     * @return
     */
    @Override
    public R list() {
        //声明数量
        int limit = 4 ; // 至多查询四条

        //查询数据库
//        IPage<Carousel> iPage = new Page<>(1,limit);
        QueryWrapper<Carousel> carouselQueryWrapper = new QueryWrapper<>();
        carouselQueryWrapper.orderByDesc("priority");

//        IPage<Carousel> page = carouselMapper.selectPage(iPage, carouselQueryWrapper);
//        List<Carousel> carouselList = page.getRecords();
        List<Carousel> list = carouselMapper.selectList(carouselQueryWrapper);
        List<Carousel> collect = list.stream().limit(6).collect(Collectors.toList());


        return R.ok(collect);
    }
}
